package com.hxzy.yygh.user.api;

import com.hxzy.yygh.common.result.Result;
import com.hxzy.yygh.common.utils.AuthContextHolder;
import com.hxzy.yygh.model.user.Patient;
import com.hxzy.yygh.user.service.PatientService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

//就诊人管理接口
@Api(tags = "就诊人管理接口")
@RestController
@RequestMapping("/api/user/patient")
public class PatientApiController {
    @Autowired
    private PatientService patientService;

    //获得就诊人列表方法
    @ApiOperation(value = "获得就诊人列表方法")
    @GetMapping("auth/findAll")
    public Result findAll(HttpServletRequest request){
        //获取当前用户id
        Long userId = AuthContextHolder.getUserId(request);
        //根据用户id获取就诊人列表
        List<Patient> patientList = patientService.getPatientList(userId);
        //返回就诊人列表
        return Result.ok(patientList);
    }

    //添加就诊人方法
    @ApiOperation(value = "添加就诊人方法")
    @PostMapping("auth/save")
    public Result savePatient(@RequestBody Patient patient, HttpServletRequest request){
        //获取当前用户
        Long userId = AuthContextHolder.getUserId(request);
        //封装用户id参数
        patient.setUserId(userId);
        //调用新增方法
        patientService.save(patient);
        //返回成功状态码
        return Result.ok();
    }

    //修改就诊人方法
    @ApiOperation(value = "修改就诊人方法")
    @PutMapping("auth/update")
    public Result updatePatient(@RequestBody Patient patient){
        //调用修改方法
        patientService.updateById(patient);
        //返回成功状态码
        return Result.ok();
    }


    //删除就诊人方法
    @ApiOperation(value = "删除就诊人方法")
    @DeleteMapping("auth/remove/{id}")
    public Result updatePatient(@PathVariable Long id) {
        //调用删除方法
        patientService.removeById(id);
        //返回成功状态码
        return Result.ok();
    }

    //根据id查询就诊人信息方法
    @ApiOperation(value = "根据id查询就诊人信息方法")
    @GetMapping("auth/getById/{id}")
    public Result getPatientById(@PathVariable Long id) {
        //调用根据id查询方法
       Patient patient = patientService.getPatientById(id);
        //返回成功状态码
       return Result.ok(patient);
    }

    //根据id查询就诊人信息方法(内部调用)
    @GetMapping("/inner/getPatientById/{id}")
    public Patient getInnerPatientById(@PathVariable Long id){
        return patientService.getPatientById(id);
    }



}

